package com.zbj.sleuth.provider;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * SleuthProviderController
 *
 * @author weigang
 * @create 2019-08-21
 **/
@RestController
@Slf4j
public class SleuthProviderController {

    @GetMapping("hello")
    private String hello(@RequestParam(value = "world", required = false, defaultValue = "world") String world,
                         HttpServletRequest request) {
        // 官方文档为大写 X-B3-TraceId X-B3-SpanId  https://cloud.spring.io/spring-cloud-static/spring-cloud-sleuth/2.1.2.RELEASE/single/spring-cloud-sleuth.html
        String traceId = request.getHeader("x-b3-traceid");
        String spanId = request.getHeader("x-b3-spanid");
        // 需要输出日志才打印 traceId 和 spanId
        // 2019-08-22 11:47:55.527  INFO [sleuth-provider,fa81a9819ee0570d,fdd1afbab383e012,false] 4160 --- [nio-8095-exec-1] c.z.s.provider.SleuthProviderController  : consumer start traceId-> fa81a9819ee0570d spanId-> fdd1afbab383e012
        log.info("provider start traceId-> {} spanId-> {}", traceId, spanId);
        return "hello " + world;
    }
}